<template>
  <div class="warpper_containner">
    <div class="from_login">
      <div class="title">
        <img src="../assets/logo.png" alt="" />
      </div>
      <el-form ref="form" :model="form" class="demo-reuleFrom">
        <el-form-item>
          <el-input v-model="user_name" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item :error="errorMessage">
          <el-input
            type="password"
            v-model="password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <!-- <el-input v-model="code"></el-input> -->
          <VerifiCode
            
            @returnRes="returnRes"
          ></VerifiCode>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            size="medium"
            :loading="loading"
            @click="handleClick"
            @keyup.enter.native="handleClick"
            style="width: 100%"
            round
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { reactive, ref, toRefs, defineComponent } from "vue";
import { ElMessage } from "element-plus";
import { getCurrentTime } from "../utils/Time/index.js";
import { post, get } from "../api/api.js";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
import VerifiCode from "@/components/verifiCode";
export default defineComponent({
  components: {
    VerifiCode,
  },
  setup() {
    const form = reactive({ user_name: "", password: "", code: "" });
    const loading = ref(false); // 显示loading
    const user_id = ref(""); // 用户id
    const store = useStore();
    const router = useRouter();
    const errorMessage = ref(""); // 错误信息
    // 点击登录按钮
    const handleClick = async () => {
      loading.values = true;
      let isFlag = Object.values(form).some((item) => item != "");
      if (!isFlag) {
        ElMessage({
          message: "请输入用户名称和密码",
          type: "error",
        });
        return;
      }

      let res = await post("/users/login", form);
      if (res.code == 0) {
        ElMessage({
          message: res.msg,
          type: "success",
        });
        user_id.values = res.user_id; // 设置userid
        store.commit("setToken", res.token); // 设置token的值到store
        store.commit("setLoginTime", getCurrentTime());
        getUserInfo(); // 获取用户信息
      } else {
        errorMessage.value = res.msg; // 设置错误信息
      }
    };
    // 获取验证码
    const returnRes = (val) => {
      console.log(val)
    };
    // 获取用户信息
    const getUserInfo = async () => {
      let res = await get("/info/Details", { user_id: user_id.values });
      if (res.code == 0) {
        store.commit("setUserInfo", res.result);
        router.push("/home"); // 跳转到首页
      }
    };

    return {
      ...toRefs(form),
      handleClick,
      getUserInfo,
      loading,
      user_id,
      errorMessage,
    };
  },
});
</script>

<style lang="scss">
.warpper_containner {
  .title {
    height: 150px;
    text-align: center;
    margin-bottom: 10px;
    img {
      height: 100%;
      border-radius: 50%;
    }
  }
  position: relative;
  width: 100%;
  height: 100%;
  background: url("../assets/bg.jpg") no-repeat;
  .from_login {
    padding: 20px 30px;
    position: absolute;
    left: 50%;
    top: 50%;
    min-width: 500px;
    transform: translate(-50%, -50%);
    background-color: rgba(255, 255, 255, 0.2);
    border-radius: 20px;
    .demo-reuleFrom {
      width: 60%;
      margin: 0 auto;
      .el-form-item {
        margin-bottom: 30px;
        .el-input >>> .el-input__inner {
          background-color: rgba($color: #fff, $alpha: 0.6);
          padding: 0 40px;
          text-align: center;
          font-size: 18px;
          color: #fff;
          &::placeholder {
            font-size: 16px;
            // color: #fff;
          }
        }

        .el-form-item__content {
          position: relative;
          border-radius: 20px;
          color: #fff;
          & > span {
            position: absolute;
            left: 10px;
            top: 3px;
            z-index: 20;
          }

          .el-button {
            width: 100%;
            span {
              text-align: center;
            }
          }
        }
      }
    }
  }
}
</style>
